package com.qlzb.taskmanagement.utils;


import com.qlzb.taskmanagement.vo.quartzVo.AreaDefineParam;
import com.qlzb.taskmanagement.vo.quartzVo.AreaUser;
import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tools {
    private static Logger logger = LoggerFactory.getLogger(Tools.class);
    /**
     * 日期格式判断
     * @param date
     * @return
     */
    public boolean isDate(String date) {
        String rexp = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(date);
        boolean dateType = mat.matches();
        return dateType;
    }


    /**
     * 封装请求参数
     * @param query
     * @return
     */
    public static Map<String, String> valueParse(String query) {
        if (query == null) {
            query = "";
        }
        Map<String, String> result = new LinkedHashMap<String, String>();
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            if (pair.length > 1) {
                result.put(pair[0], pair[1]);
            } else {
                result.put(pair[0], "");
            }
        }
        return result;
    }
    /**
     * 封装请求参数 区域个性化参数 替换
     * @param query
     * @return
     */
    public static Map<String, String> valueParse(String query, AreaUser areaUser, String type) {
        Map<String, String> result=Tools.valueParse(query);
        //个性化参数
       AreaDefineParam[]  areaDefineParams= areaUser.areaDefineParam;
        if(areaDefineParams!=null&&areaDefineParams.length>0){
            for(AreaDefineParam areaDefineParam: areaDefineParams){
                if(areaDefineParam.type.equals(type))
                {
                    Map<String, String> defineresult=Tools.valueParse(areaDefineParam.getPrams());
                    result.putAll(defineresult);
                }
            }
        }
        return result;
    }

    /**
     * 封装请求参数 区域个性化参数 替换
     * @param result
     * @return
     */
    public static Map<String, String> valueParse(Map<String, String> result,AreaUser areaUser,String type) {
        //个性化参数
        AreaDefineParam[]  proviceDefineParams= areaUser.proviceUser.areaDefineParam;
        AreaDefineParam[]  areaDefineParams= areaUser.areaDefineParam;
        if(areaDefineParams!=null&&areaDefineParams.length>0){
            for(AreaDefineParam areaDefineParam: areaDefineParams){
                if(areaDefineParam.type.equals(type))
                {
                    Map<String, String> defineresult=Tools.valueParse(areaDefineParam.getPrams());
                    result.putAll(defineresult);
                }
            }
        }
        if(proviceDefineParams!=null&&proviceDefineParams.length>0){
            for(AreaDefineParam areaDefineParam: proviceDefineParams){
                if(areaDefineParam.type.equals(type))
                {
                    Map<String, String> defineresult=Tools.valueParse(areaDefineParam.getPrams());
                    result.putAll(defineresult);
                }
            }
        }
        return result;
    }
    public static void  warchKeyValue(Map<String, String> map){
        for(Map.Entry<String, String>  enrry:  map.entrySet()){
                logger.info(enrry.getKey()+"  "+enrry.getValue());
        }
    }
    public static Boolean isNotBlank(String str){
        if (str !=null && str !=""){
            return true;
        }
        return false;
    }

    /**
     * 计算是否有下一页
     * @param pageLink
     * @return
     */
    public static boolean hasNextPage(Element pageLink){
        Element nextpage = pageLink.nextElementSibling();
        //是否下页被禁用
        Elements classname= nextpage.getElementsByClass("rich-datascr-button-dsbld");
        if(classname==null||classname.size()==0){
            return true;
        }
        return false;
    }
    /**
     * 计算总记录数
     * @param pageLink
     * @return
     */
    public static int getTotalCount(Element pageLink){
        String pageStr = pageLink.text().substring(3);
        String subPage = pageStr.substring(pageStr.indexOf("页") + 1, pageStr.length());
        String regex = "[^0-9]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(subPage);
        int pageNums = Integer.parseInt(m.replaceAll("").trim());
        return pageNums;
    }
    public static String getFormatDate(String format) {

        return getFormatDateTime(new Date(), format);

    }
    /**
     * 根据给定的格式与时间(Date类型�?)，返回时间字符串。最为�?�用�?<br>
     *
     * @param date   指定的日�?
     * @param format 日期格式字符�?
     * @return String 指定格式的日期字符串.
     */

    public static String getFormatDateTime(Date date, String format) {
        //add by lqin 2017-08-14 添加默认值
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd";
        }
        //===========add-end============
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date == null) {
            return "";
        }
        return sdf.format(date);

    }
}
